package com.braveman.im.ui.fragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Environment;
import android.os.Message;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import com.braveman.im.R;
import com.braveman.im.bean.AdvertisementBean;
import com.braveman.im.bean.GroupInfoBean;
import com.braveman.im.bean.GroupListInfoBean;
import com.braveman.im.bean.LoginBean;
import com.braveman.im.bean.TextyApiBean;
import com.braveman.im.constant.Constants;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.thread.CompressPhotoThread;
import com.braveman.im.ui.activity.GroupImActivity;
import com.braveman.im.ui.activity.HomeActivity;
import com.braveman.im.ui.activity.LocationActivity;
import com.braveman.im.ui.activity.SplashActivity;
import com.braveman.im.ui.activity.message.LoginActivity;
import com.braveman.im.ui.activity.message.LoginHXActivity;
import com.braveman.im.ui.activity.message.MainHXActivity;
import com.braveman.im.ui.activity.message.RegisterNextActivity;
import com.braveman.im.ui.activity.message.TextAc;
import com.braveman.im.ui.weiget.CustomPopwindow;
import com.braveman.im.ui.weiget.DialogUtils;
import com.braveman.im.utils.PermissionUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ht.baselibs.core.EventCode;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseActivity;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;
import com.ht.baselibs.utils.LogUtils;
import com.ht.baselibs.utils.SharedPreferencesUtils;
import com.ht.baselibs.utils.UIUtils;
import com.huawei.hms.api.Api;
import com.igexin.sdk.PushManager;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.io.File;

import retrofit2.Call;
import retrofit2.Response;

import static android.app.Activity.RESULT_OK;

public class ShoppingCircleFragment extends BaseFragment {
    private static final String TAKE_PHOTO_PATH = "ht/jkm/photo/LICENCE.jpg";
    private static final int REQUEST_CODE_TAKE_PHOTO = 1;
    private static final int REQUEST_CAMERA_CODE = 2;
    private static final int MY_PERMISSIONS_REQUEST_CALL_CAMERA = 0;
    private static final int MY_PERMISSIONS_REQUEST_CALL_WRITE_SDCARD = 1;
    private String picturePath;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_home, null);
    }

    @Override
    protected void initChildView() {

    }

    @Override
    protected void load() {

    }


    @Override
    protected void setChildViewListener() {
        findViewById(R.id.ll_location).setOnClickListener(this);
        findViewById(R.id.ll_share).setOnClickListener(this);
        findViewById(R.id.ll_im_only).setOnClickListener(this);
        findViewById(R.id.ll_im_group).setOnClickListener(this);
        findViewById(R.id.ll_group_for_mine).setOnClickListener(this);
        findViewById(R.id.ll_getui).setOnClickListener(this);
        findViewById(R.id.ll_splash).setOnClickListener(this);
        findViewById(R.id.go_logout).setOnClickListener(this);
        findViewById(R.id.yang_hao_text).setOnClickListener(this);
        findViewById(R.id.tv_photo).setOnClickListener(this);
        findViewById(R.id.api_text).setOnClickListener(this);
        findViewById(R.id.tv_text).setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_location:
                ActivityUtils.startActivity(mContext, LocationActivity.class);
                break;
            case R.id.ll_share:

                // TODO: 17/9/28 分享还没搞
//                UMImage image = new UMImage(mContext, "http://7xk9dj.com1.z0.glb.clouddn.com/refreshlayout/images/staggered11.png");//网络图片
////                UMImage image = new UMImage(mContext, file);//本地文件
////                UMImage image = new UMImage(mContext, R.drawable.xxx);//资源文件
////                UMImage image = new UMImage(mContext, bitmap);//bitmap文件
////                UMImage image = new UMImage(mContext, byte[]);//字节流
//                UMImage thumb = new UMImage(mContext, R.mipmap.ic_launcher);
//                image.setThumb(thumb);
//
//
//                UMWeb web = new UMWeb("https://www.baidu.com");
//                web.setTitle("This is music title");//标题
//                web.setThumb(thumb);  //缩略图
//                web.setDescription("my description");//描述
//
//
//                new ShareAction(mContext)
//                        .withText("hello").withMedia(web)
//                        .setDisplayList(SHARE_MEDIA.QQ, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE)
//                        .setCallback(shareListener)
//                        .open();
                break;
            case R.id.ll_im_only:
                boolean login = SharedPreferencesUtils.getBool("login", false);
                ActivityUtils.startActivity(mContext, login ? MainHXActivity.class : LoginHXActivity.class);
                break;
            case R.id.ll_im_group:
                ApiManager.getApi().getGroupList().enqueue(new NetCallBack<GroupInfoBean>() {
                    @Override
                    protected void onSuccess(Call<GroupInfoBean> call, Response<GroupInfoBean> response) {
                        GroupInfoBean body = response.body();
                    }
                });
                break;
            case R.id.ll_getui:
                String cid = PushManager.getInstance().getClientid(mContext);
                UIUtils.showToast(!TextUtils.isEmpty(cid) ? "推送配置成功当前设备=" + cid : "推送配置错误，请重新配置");
                Log.e("cid = ", cid);
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        boolean alias_test = PushManager.getInstance().bindAlias(mContext, "alias_test_android");
                        UIUtils.showToast(alias_test ? "绑定成功" : "绑定失败");
                    }
                });
                thread.start();

                break;
            case R.id.ll_splash:
                ActivityUtils.startActivity(mContext, SplashActivity.class);
                break;
            case R.id.go_logout:
                ActivityUtils.startActivity(mContext, SplashActivity.class);
                ApiManager.getApi().loginOut().enqueue(new NetCallBack<String>() {
                    @Override
                    protected void onSuccess(Call<String> call, Response<String> response) {
                        SharedPreferencesUtils.put(Constants.TOKEN, "");
                        SharedPreferencesUtils.put("isLogin", false);
                        UIUtils.showToast("退出成功");
                        ActivityUtils.startActivityAndFinish(mContext, LoginActivity.class);
                    }
                });

                break;
            case R.id.yang_hao_text:
                // TODO: 17/10/12 杨昊自测
                break;
            case R.id.api_text:
                TextyApiBean textyApiBean = new TextyApiBean();
                textyApiBean.captchaType = "memberRegister";
                textyApiBean.mobile = "15264880529";
                ApiManager.getApi().textApi(textyApiBean).enqueue(new NetCallBack<AdvertisementBean>() {
                    @Override
                    protected void onSuccess(Call<AdvertisementBean> call, Response<AdvertisementBean> response) {
                        if (response != null && response.body() != null)
                            UIUtils.showToast(response.body().captcha + "");
                    }
                });

                break;
            case R.id.tv_photo:
                ActivityUtils.startActivity(mContext, RegisterNextActivity.class);
                break;
            case R.id.tv_text:
                ActivityUtils.startActivity(mContext, TextAc.class);
                break;

        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {

                case REQUEST_CODE_TAKE_PHOTO:
                    String filePath = Environment.getExternalStorageDirectory() + "/" + TAKE_PHOTO_PATH;
                    new CompressPhotoThread(mHandler, filePath, 0).start();
                    break;
                //                 选择照片
                case REQUEST_CAMERA_CODE:
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = mContext.getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    new CompressPhotoThread(mHandler, picturePath, 1).start();
                    break;

            }
        }
    }

    @Override
    protected void onVisible() {
    }

    @Override
    protected boolean hasEvent() {
        return true;
    }

    @Override
    protected void onEvent(int eventCode, Object data) {
        super.onEvent(eventCode, data);
        switch (eventCode) {
            case EventCode.HSY_REQUEST_CODE_EXPIRE://登陆超时

                break;

        }
    }

    private UMShareListener shareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(mContext, "成功了", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(mContext, "失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(mContext, "取消了", Toast.LENGTH_LONG).show();

        }
    };

}
